{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.instrument import EqOption, OptionType, OptionStyle\n",
    "from gs_quant.backtests.strategy import Strategy\n",
    "from gs_quant.backtests.triggers import *\n",
    "from gs_quant.backtests.actions import *\n",
    "from gs_quant.backtests.equity_vol_engine import *\n",
    "from gs_quant.target.common import UnderlierType\n",
    "from datetime import date\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Define backtest dates\n",
    "start_date = date(2019, 9, 4)\n",
    "end_date = date(2020, 9, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Define option instrument for strategy\n",
    "option = EqOption('SX5E', underlierType=UnderlierType.BBID, expirationDate='3m', strikePrice='ATM', optionType=OptionType.Call, optionStyle=OptionStyle.European)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Define a periodic trade action.  Trade 1000 units of the option instrument and hold for 1m\n",
    "trade_action = EnterPositionQuantityScaledAction(priceables=option, trade_duration='1m', trade_quantity=1000, trade_quantity_type=BacktestTradingQuantityType.quantity)\n",
    "\n",
    "# Define an entry signal trigger.  This is a list of dates when the entry signal is true.\n",
    "# When entry signal is true, the strategy will enter a position (only if no position is already held)\n",
    "entry_dates = (date(2019,9,20), date(2019,10,18), date(2019,11,15), date(2019,12,20), date(2020,1,17), date(2020,2,21))\n",
    "entry_trigger = AggregateTrigger(trigger_requirements=AggregateTriggerRequirements([\n",
    "    DateTriggerRequirements(dates=entry_dates),\n",
    "    PortfolioTriggerRequirements('len', 0, TriggerDirection.EQUAL)]), \n",
    "                                 actions=trade_action)\n",
    "\n",
    "# Define an exit signal trigger.  This is a list of dates when the exit signal is true.\n",
    "# When the exit signal is true the strategy will exit the full position (only if already holding a position)\n",
    "exit_dates = (date(2019,11,4), date(2020,3,2))\n",
    "exit_trigger = AggregateTrigger(trigger_requirements=AggregateTriggerRequirements([\n",
    "    DateTriggerRequirements(dates=exit_dates),\n",
    "    PortfolioTriggerRequirements('len', 0, TriggerDirection.ABOVE)]), actions=ExitTradeAction())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create strategy\n",
    "strategy = Strategy(initial_portfolio=None, triggers=[entry_trigger, exit_trigger])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run backtest\n",
    "backtest = EquityVolEngine.run_backtest(strategy, start=start_date, end=end_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot the performance\n",
    "pnl = backtest.get_measure_series(FlowVolBacktestMeasure.PNL)\n",
    "pnl.plot(legend=True, label='PNL Total')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
